<?php
/**
 * 影投管理
 */
class Action
{
    private $per_page = 20;

    /**
     * 影投报表
     * @author xiaolin
     * @date   2016-10-16
     * @return
     */
    public function index() {
        $store_ids = $this->get_store_ids();

        $deliver_order_num = order_helper::get_order_num_by_progress(array(5), $type = 5, array('store_id' => $store_ids));
        $receiver_order_num = order_helper::get_order_num_by_progress(array(10), $type = 5, array('store_id' => $store_ids));
        $cancel_order_num = order_helper::get_order_num_by_progress(array(15), $type = 5, array('store_id' => $store_ids));
        $all_order_num = order_helper::get_order_num_by_progress(array(1,3,5,10,15), $type = 5, array('store_id' => $store_ids));
        $all_money_num = _widget('stat.api_stat')->get_store_sale_data($store_ids, 'all', 'price');

        $store_half_year_order_data = store_helper::get_half_year_order_by_store_id($store_ids);

        $res = array(
            'store_num' => $store_ids != -1 ? count($store_ids) : 0,
            'purchase_money' => format_money($all_money_num), //累计进货总金额
            'deliver_order_num' => $deliver_order_num, //配送中订单总数
            'receiver_order_num' => $receiver_order_num, //已收货订单总数
            'cancel_order_num' => $cancel_order_num, //撤销订单总数
            'all_order_num' => $all_order_num, //所有订单总数
            'half_year' => json_encode(array_column($store_half_year_order_data, 'month')),
            'price_sum' => json_encode(array_column($store_half_year_order_data, 'price_sum')),
            'order_sum' => json_encode(array_column($store_half_year_order_data, 'order_sum'))
        );

        Response::assign('data', $res);
        Response::display('admin/index.html');
    }

    /**
     * 订单列表
     * @author xiaolin
     * @date   2016-10-16
     * @return
     */
    public function order_list() {
        $page_no = $progress = $search_filter = null;

        extract(URLTracker::init(array(
            'page_no' => 1,
            'progress' => '-1',
            'search_filter' => array(),
        )));
        
        // 拼装搜索条件
        $filter['is_enable'] = 1;
        $filter['status'] = 1;
        $filter['type'] = 5;
        $filter['store_id'] = $this->get_store_ids();
        
        if ($progress != -1) {
            $filter['progress'] = $progress;
        }

        if (isset($search_filter['order_code']) && $search_filter['order_code']) {
            $decoded_id = order_helper::order_id_decode($search_filter['order_code']);
            $filter['id'] = $decoded_id ? $decoded_id : -1;
        }

        if (isset($search_filter['user_name']) && $search_filter['user_name']) {
            $user_info = user_helper::get_user_info_by_username($search_filter['user_name']);
            $filter['user_id'] = $user_info ? $user_info['id'] : -1;
        }

        // 下单时间
        if (isset($search_filter['start_date_add']) && $search_filter['start_date_add']) {
            $filter['add_time >='] = $search_filter['start_date_add'] . ' 00:00:00';
        }
        if (isset($search_filter['end_date_add']) && $search_filter['end_date_add']) {
            $filter['add_time <='] = $search_filter['end_date_add'] . ' 23:59:59';
        }

        $order_list = array();
        $order_count = _model('order')->getTotal(enable_filter($filter));
        if ($order_count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($order_count)) {
                Response::assign('pager', $pager);
            }

            $order_list = _model('order')->getList(enable_filter($filter), ' ORDER BY `id` DESC ' . $pager->getLimit($page_no));
            foreach ($order_list as $k => $v) {
                $order_list[$k]['order_code'] = order_helper::order_id_encode($v['id']);
                $order_list[$k]['price'] = format_money($v['price']);
                $order_list[$k]['pay_user_money'] = format_money($v['pay_user_money']);

                //代理名称
                $order_list[$k]['task_user_name'] = $v['task_user_id'] ? user_helper::display_name($v['task_user_id']) : '';

                //下单用户
                $order_list[$k]['user_info'] = user_helper::get_user_info($v['user_id']);
                $order_list[$k]['user_role_id'] = user_helper::get_user_role_id($v['user_id']);

                //店铺信息
                $store_info = store_helper::get_store($v['store_id']);
                $order_list[$k]['store_info'] = $store_info;

                //省市区信息
                $province_info = $store_info ? area_helper::get_area_info($store_info['province_id']) : '';
                $city_info = $store_info ? area_helper::get_area_info($store_info['city_id']): '';
                $county_info = $store_info ? area_helper::get_area_info($store_info['district_id']) : '';
                $order_list[$k]['province_info'] = $province_info ? $province_info['area'] : '';
                $order_list[$k]['city_info'] = $city_info ? $city_info['area'] : '';
                $order_list[$k]['county_info'] = $county_info ? $county_info['area'] : '';

                //合伙人
                $order_list[$k]['dealer_info'] = user_helper::get_user_info($v['agency_id']);

                $order_relation = _model('order_relation')->getList(array(
                    'order_id' => $v['id'],
                    'status' => 1
                ));
                if ($order_relation) {
                    foreach ($order_relation as $key => $orv) {
                        $relation_goods_info = goods_helper::get_goods_info($orv['goods_id']);
                        $relation_goods_info['category_title'] = _uri('category', $relation_goods_info['category_id'], 'title');
                        $relation_goods_info['goods_price'] = format_money($orv['goods_price']);
                        $relation_goods_info['real_price'] = format_money($orv['real_price']);

                        $order_relation[$key]['relation_goods_info'] = $relation_goods_info;
                    }
                }
                $order_list[$k]['order_relation'] = $order_relation;
                $order_list[$k]['order_relation_count'] = count($order_relation);
            }
        }

        Response::assign('order_list', $order_list);
        Response::assign('progress', $progress);
        Response::assign('search_filter', $search_filter);

        Response::display('admin/order_list.html');
    }

    /**
     * 影投管理的影院列表
     * @author xiaolin
     * @date   2016-10-17
     * @return
     */
    public function cinema_list() {
        $page_no = null;

        extract(URLTracker::init(array(
            'page_no' => 1,
        )));

        $filter['id'] = $this->get_store_ids();
        $store_list = get_data_list('store', enable_filter($filter), ' order BY `id` DESC ', $page_no, $this->per_page);

        if ($store_list) {
            $type_arr = store_config::$type_arr;
            foreach ($store_list as $k => $v) {
                if ($v['owner_id'] > 0) {
                    $user_info = _uri('user', $v['owner_id']);
                    if ($user_info) {
                        $store_list[$k]['user_name'] = $user_info['user_name'];
                        $store_list[$k]['mobile'] = $user_info['mobile'];
                    }
                }
                if (isset($type_arr[$v['type']])) {
                    $store_list[$k]['type_name'] = $type_arr[$v['type']];
                }
                if ($v['agency_id']) {
                    $dealer_info = _uri('user', $v['agency_id']);
                    if ($dealer_info) {
                        $store_list[$k]['dealer_name'] = $dealer_info['realname'];
                        $store_list[$k]['dealer_mobile'] = $dealer_info['mobile'];
                    }
                }
                if ($v['cover']) {
                    $store_list[$k]['cover'] = _image($v['cover']);
                } else {
                    $store_list[$k]['cover'] = store_helper::cover($v['cover']);
                }
            }
        }

        Response::assign('store_list', $store_list);
        Response::display('admin/store_list.html');
    }

    /**
     * 获取登录的影投管理员对应的影院id
     * @author xiaolin
     * @date   2016-10-17
     * @return array
     */
    private function get_store_ids() {
        $filter_store['type'] = array(100, 105);
        $filter_store['status !='] = 0;
        $filter_store['is_delete'] = 1;
        $filter_store['is_enable'] = 1;
        $filter_store['cinema_investment_manager_id'] = $_SESSION['user_id'];
        $store_ids = _model('store')->getFields('id', enable_filter($filter_store));
        if (!$store_ids) {
            return -1;
        }
        return $store_ids;
    }
}